package com.bng8.superstar_cms.exception;

import com.bng8.superstar_cms.domain.common.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * ClassName: GlobalExceptionHandler
 * Package: com.bng8.superstar_cms.exception
 * Date: 2023/8/15 8:51
 * Author: 安知（13921877634）
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 拦截业务类异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    public ResponseResult businessExceptionHandle(BusinessException e) {
        log.error("捕捉到业务类异常：", e);

        return ResponseResult.failure(e.getCode(), e.getMessage());
    }


    /**
     * 拦截运行时异常
     *
     * @param e
     */
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseResult runtimeExceptionHandle(RuntimeException e) {
        log.error("捕捉到运行时异常：", e);

        return ResponseResult.failure(
                ErrorCodeEnum.UNKNOWN_ERROR.getCode(),
                e.getMessage());
    }

    /**
     * 捕捉系统级异常
     *
     * @param th
     * @return
     */
    @ExceptionHandler(value = Throwable.class)
    public ResponseResult throwableHandle(Throwable th) {
        log.error("捕捉Throwable异常：", th);

        return ResponseResult.failure(
                ErrorCodeEnum.SYSTEM_ERROR.getCode(),
                th.getMessage());
    }
}
